package seiacpjava_a2.utilities;

public class MemoryConsumptionCalculator {
	
	long memoryUsedBeforeOperation;
	long memoryUsedAfterOperation;
	
	
	public MemoryConsumptionCalculator()
	{
		this.memoryUsedBeforeOperation = 0L;
		this.memoryUsedAfterOperation = 0L;
	}
	
	public void recordMemoryUsedBeforeOperation()
	{ 
		this.memoryUsedBeforeOperation = recordMemoryUsed();   
	}
	
	public void recordMemoryUsedAfterOperation()
	{ 
		this.memoryUsedAfterOperation = recordMemoryUsed();   
	}

	private long recordMemoryUsed() {
//		System.gc(); System.gc(); System.gc(); System.gc();
//	    System.gc(); System.gc(); System.gc(); System.gc();
//	    System.gc(); System.gc(); System.gc(); System.gc();
//	    System.gc(); System.gc(); System.gc(); System.gc();
	    
	    return (Runtime.getRuntime().totalMemory() -
	    		 	  Runtime.getRuntime().freeMemory());
	}
	
	
	public long getMemoryConsumption()
	{
		return this.memoryUsedAfterOperation - this.memoryUsedBeforeOperation;
	}
	

}
